/* eslint-disable */
console.log("vue.config.js", process.env.NODE_ENV);
const AutoDllPlugin = require("autodll-webpack-plugin");
const chalk = require('chalk')
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const CompressionWebpackPlugin = require('compression-webpack-plugin');
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
let IS_PROD = process.env.NODE_ENV === 'production';
module.exports = {
    css: {
        loaderOptions: {
            less: {
                // todo: 修改主题色 需求删除
                additionalData: `@bg:#fff;`
            }
        }
    },

    productionSourceMap: false,
    publicPath: process.env.NODE_ENV === 'production' ? '/dist/' : '',
    // pluginOptions: {
    //     compression: {
    //         gzip: {
    //             filename: '[file].gz[query]',
    //             algorithm: 'gzip',
    //             include: /\.(js|css|html|svg|json)(\?.*)?$/i,
    //             minRatio: 0.8
    //         }
    //     }
    // },
    configureWebpack: (config) => {
        // config.mode = 'development';
        config.devtool = 'source-map';
        if (IS_PROD) {
            // 启用 gzip 压缩插件
            config.plugins.push(
                new CompressionWebpackPlugin({
                    test: /\.js$|\.html$|\.css$/u,
                    threshold: 4096 // 超过 4kb 压缩
                }),
                // 测试打包时间
                new ProgressBarPlugin({
                    format: ' build [:bar] ' + chalk.green.bold(':percent') + ' (:elapsed seconds)',
                    clear: false
                }),
                // 提升打包效率
                new AutoDllPlugin({
                    inject: true, // 在html中引入
                    filename: '[name]_[hash].dll.js',
                    entry: {
                        vendor: ['vue', 'element-ui', 'vuex', 'vue-router']
                    }
                }),
                new BundleAnalyzerPlugin()
            );
            config.optimization = {
                splitChunks: {
                    cacheGroups: {
                        common: {
                            name: 'chunk-common', // 打包后的文件名
                            chunks: 'initial', //
                            minChunks: 2,
                            maxInitialRequests: 5,
                            minSize: 0,
                            priority: 1,
                            reuseExistingChunk: true
                        },
                        vendors: {
                            name: 'chunk-vendors',
                            test: /[\\/]node_modules[\\/]/,
                            chunks: 'initial',
                            priority: 2,
                            reuseExistingChunk: true,
                            enforce: true
                        },
                        element: {
                            name: 'element',
                            test: /[\\/]node_modules[\\/]element-ui[\\/]/,
                            chunks: 'initial',
                            priority: 3,
                            reuseExistingChunk: true,
                            enforce: true
                        }
                    }
                }
            };
            config.output.filename = 'js/[name].[contenthash:4].js';
            config.output.chunkFilename = 'js/[name].[contenthash:4].js';
            // eslint-disable-next-line no-empty
        } else {
            config.mode = 'development';
            config.devtool = 'source-map';
            config.plugins.push(
                // 测试打包时间
                new ProgressBarPlugin({
                    format: ' build [:bar] ' + chalk.green.bold(':percent') + ' (:elapsed seconds)',
                    clear: false
                })
                // 提升打包效率
                // new AutoDllPlugin({
                //     inject: true, // 在html中引入
                //     debug: true,
                //     filename: '[name]_[hash].dll.js',
                //     entry: {
                //         vendor: ['vue', 'element-ui', 'vuex', 'vue-router']
                //     }
                // })
            );
        }
    },
    devServer: {
        proxy: {
            '/video/test/': {
                target: 'https://v26-web.douyinvod.com/d59d563bb794c68e2878e714b1ee4d24/62e8976c/video/tos/cn/tos-cn-ve-15c001-alinc2/66100f70aa10450c85cb2dd54c1c3a18/?a=6383&ch=26&cr=0&dr=0&lr=all&cd=0%7C0%7C0%7C0&cv=1&br=1599&bt=1599&cs=0&ds=4&ft=5q_lc5mmnPD12N-ira.-UxPEFaY3c3wv25Qa&mime_type=video_mp4&qs=0&rc=aGZmZTk0ODZoPDVlPDw6M0BpanJwdDw6Zm8zZTMzNGkzM0AuLmFfNDAwNWMxM2FhMDEwYSNlZW1rcjRnb2VgLS1kLWFzcw%3D%3D&l=021659406617558fdbddc0200ff2f010a9ebd880000008f375d74',
                headers: {
                    // 如果需要传递cookie，可以在此添加
                    // Cookie: cookies
                }
            },
            '/api/': {
                target: 'http://ait.dev.weiyun.test.com/',
                headers: {
                    // 如果需要传递cookie，可以在此添加
                    // Cookie: cookies
                }
            },
            '/books/': {
                target: 'https://movie.douban.com/j/search_subjects?type=tv&tag=%E7%83%AD%E9%97%A8&page_limit=50&page_start=0',
                headers: {
                    // 如果需要传递cookie，可以在此添加
                    // Cookie: cookies
                }
            }
        },
        overlay: {
            warnings: true,
            errors: true
        }
    }
};

